package com.surfacing.rcp.server.listener;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.surfacing.rcp.db.dao.JournalDAO;
import com.surfacing.rcp.db.dao.factory.DaoFactory;
import com.surfacing.rcp.model.QueryFilter;
import com.surfacing.rcp.model.common.AbstractMessage;
import com.surfacing.rcp.model.common.IFilterTypes;
import com.surfacing.rcp.model.entity.DAOException;
import com.surfacing.rcp.model.entity.Journal;

/**
 * This class will be notified uppon Journal changes
 * 
 */
public class JournalListener extends ServerListenerAdapter {
	private JournalDAO staffDao = DaoFactory.MYSQL.createJournalDAO();

	@Override
	public AbstractMessage handleAddRequest(AbstractMessage addObject)
			throws DAOException, SQLException {
		Journal addMember = (Journal) addObject;
		if (!staffDao.addJournal(addMember))
			throw new DAOException("JournalListener",
					"Failed to add the Journal: " + addMember);
		return addMember;
	}

	/**
	 * Update of a Journal
	 */
	@Override
	public AbstractMessage handleUpdateRequest(AbstractMessage updateObject)
			throws DAOException, SQLException {
		Journal updateMember = (Journal) updateObject;
		if (!staffDao.updateJournal(updateMember))
			throw new DAOException("JournalListener",
					"Failed to update the Journal: " + updateMember);
		return updateMember;
	}
	
	@Override
	public AbstractMessage handleRemoveRequest(AbstractMessage removeObject)
			throws DAOException, SQLException {
		Journal updateMember = (Journal) removeObject;
		if (!staffDao.deleteJournal(updateMember))
			throw new DAOException("JournalListener",
					"Failed to delte the Journal: " + updateMember);
		return updateMember;
	}

	/**
	 * Listing of all members
	 */
	@Override
	public ArrayList<AbstractMessage> handleListingRequest(
			QueryFilter queryFilter) throws DAOException, SQLException {
		ArrayList<AbstractMessage> list = new ArrayList<AbstractMessage>();
		List<Journal> memberList;
		// if there is no filter -> request all
		if (queryFilter == null || queryFilter.getFilterList().isEmpty()) {
			memberList = staffDao.getAllJournal();
			if (memberList == null)
				throw new DAOException("JournalListener",
						"Failed to list all Journals");
			list.addAll(memberList);
		} else if (queryFilter.containsFilterType(IFilterTypes.ID_FILTER)) {
			// get the query filter
			final String filter = queryFilter
					.getFilterValue(IFilterTypes.ID_FILTER);
			int id = Integer.parseInt(filter);
			Journal member = staffDao.getJournalByID(id);
			if (member == null)
				throw new DAOException("JournalListener",
						"Failed to get the Journal by id:" + id);
			list.add(member);
		} else if (queryFilter.containsFilterType(IFilterTypes.COUNT_FILTER)) {
			// get the query filter
			final String filter = queryFilter
					.getFilterValue(IFilterTypes.COUNT_FILTER);
			int id = Integer.parseInt(filter);
			memberList = staffDao.getRangeJournal(30 * id);
			if (memberList == null)
				throw new DAOException("JournalListener",
						"Failed to list range Journals");
			list.addAll(memberList);
		}
		return list;
	}
}